package com.hdl.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;
import com.hdl.commons.DBUtil;
import com.hdl.entity.MallAddress;
/**
 * <pre>
 * - 数据访问对象
 * - 商城用户收货地址
 * ClassName : com.hdl.dao.MallAddressDAO
 * @author 湛锋
 * Date : 2020-8-24 10:40:45
 * </pre>
 */
public class MallAddressDAO {

	/**
	 * 添加一个商城用户收货地址
	 * 包含所有字段
	 * @param mallAddress - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int insert(MallAddress mallAddress) throws SQLException {
		if(mallAddress.getMallAddressId()==null) {
			mallAddress.setMallAddressId(String.valueOf(DBUtil.nextId()));
		}
		return DBUtil.insert(mallAddress);
	}
 
	
	/**
	 * 添加一个商城用户收货地址
	 * 只包含非null的字段
	 * @param mallAddress - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int insertSelective(MallAddress mallAddress) throws SQLException {
		if(mallAddress.getMallAddressId()==null) {
			mallAddress.setMallAddressId(String.valueOf(DBUtil.nextId()));
		}
		return DBUtil.insertSelective(mallAddress);
	}
	
//public int insertSelective(MallAddress mallAddress) throws SQLException {
//		
//		String sql = "INSERT INTO mall_address (ADDRESS_ID,USER_ID,ADDRESS_NAME,ADDRESS_TEL, ADDRESS_ZIP,ADDRESS_CODE,ADDRESS_INFO,ADDRESS_DEFAULT) value(?,?,?,?,?,?,?,?)";
//		try (Connection conn = JdbcUtil.getConnection(); PreparedStatement pstmt = conn.prepareStatement(sql);) {
//			JdbcUtil.setParams(pstmt, DBUtil.nextId(),mallAddress.getHdlUserId(),mallAddress.getMallAddressName(),mallAddress.getMallAddressTel(),mallAddress.getMallAddressZip(),mallAddress.getMallAddressCode(),mallAddress.getMallAddressInfo(),mallAddress.getMallAddressDefault());
//
//			return pstmt.executeUpdate();
//		}
//	}
	

	/**
	 * 根据主键更新一个商城用户收货地址
	 * 包含所有字段
	 * @param mallAddress - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int update(MallAddress mallAddress) throws SQLException {
		return DBUtil.update(mallAddress);
	}
	
	/**
	 * 根据主键更新一个商城用户收货地址
	 * 只包含非null的字段
	 * @param mallAddress - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int updateSelective(MallAddress mallAddress) throws SQLException {
		return DBUtil.updateSelective(mallAddress);
	}
	
	/**
	 * 根据主键删除一个商城用户收货地址
	 * @param addressId - 主键
	 * @return
	 * @throws SQLException
	 */
	public int deleteById(Long addressId) throws SQLException  {
		return DBUtil.execute("DELETE FROM mall_address WHERE MALL_ADDRESS_ID=?", addressId);
	}
	
	/**
	 * 根据主键获取一个商城用户收货地址
	 * @param addressId - 主键
	 * @return
	 * @throws SQLException 没有获取到，或者获取多个都会抛出异常
	 */
	public MallAddress selectById(Long addressId) throws SQLException  {
		return DBUtil.get("SELECT * FROM mall_address WHERE MALL_ADDRESS_ID=?", MallAddress.class ,addressId);
	}

		
	/**
	 * 根据模型中的非空字段查询
	 * @param addressId - 实体对象
	 * @return
	 * @throws SQLException 没有获取到，或者获取多个都会抛出异常
	 */
	public MallAddress selectByModel(MallAddress mallAddress)throws SQLException {
		StringBuffer sql = new StringBuffer("SELECT * FROM mall_address WHERE 1=1 ");
		List<Object> params = new ArrayList<Object>();
		where(mallAddress,sql,params);
		return DBUtil.get(sql.toString(), MallAddress.class, params.toArray());
	}
	
	/**
	 * 根据模型中的非空字段查询
	 * @param addressId - 实体对象
	 * @param index - 开始索引
	 * @param len - 返回长度
	 * @return
	 * @throws SQLException
	 */
	public List<MallAddress> selectByModel(MallAddress mallAddress,int index,int len)throws SQLException {
		StringBuffer sql = new StringBuffer("SELECT * FROM mall_address WHERE 1=1 ");
		List<Object> params = new ArrayList<Object>();
		where(mallAddress,sql,params);
		sql.append(" ORDER BY MALL_ADDRESS_ID DESC LIMIT ?,?");
		params.add(index);
		params.add(len);
		return DBUtil.list(sql.toString(), MallAddress.class, params.toArray());
	}

	/**
	 * 根据模型中的非空字段统计
	 * @param addressId - 实体对象
	 * @return
	 * @throws SQLException
	 */
	public long countByModel(MallAddress mallAddress)throws SQLException {
		StringBuffer sql = new StringBuffer("SELECT count(*) as c FROM mall_address WHERE 1=1 ");
		List<Object> params = new ArrayList<Object>();
		where(mallAddress,sql,params);
		return DBUtil.getLong(sql.toString(), params.toArray());
	}
	
	/**
	 * 格式化where条件和参数
	 * @param addressId - 实体对象
	 * @param sql - SQL语句
	 * @param params - SQL语句中的参数
	 */
	private void where(MallAddress mallAddress,StringBuffer sql,List<Object> params) {
		if(mallAddress==null){
			return;
		}

		if(mallAddress.getMallAddressId()!=null) {
			sql.append(" AND MALL_ADDRESS_ID=?");
			params.add(mallAddress.getMallAddressId());
		}

		if(mallAddress.getHdlUserId()!=null) {
			sql.append(" AND HDL_USER_ID=?");
			params.add(mallAddress.getHdlUserId());
		}

		if(mallAddress.getMallAddressName()!=null) {
			sql.append(" AND MALL_ADDRESS_NAME=?");
			params.add(mallAddress.getMallAddressName());
		}

		if(mallAddress.getMallAddressTel()!=null) {
			sql.append(" AND MALL_ADDRESS_TEL=?");
			params.add(mallAddress.getMallAddressTel());
		}

		if(mallAddress.getMallAddressZip()!=null) {
			sql.append(" AND MALL_ADDRESS_ZIP=?");
			params.add(mallAddress.getMallAddressZip());
		}

		if(mallAddress.getMallAddressCode()!=null) {
			sql.append(" AND MALL_ADDRESS_CODE=?");
			params.add(mallAddress.getMallAddressCode());
		}

		if(mallAddress.getMallAddressInfo()!=null) {
			sql.append(" AND MALL_ADDRESS_INFO=?");
			params.add(mallAddress.getMallAddressInfo());
		}

		if(mallAddress.getMallAddressDefault()!=null) {
			sql.append(" AND MALL_ADDRESS_DEFAULT=?");
			params.add(mallAddress.getMallAddressDefault());
		}

	}
}